package com.domain.service.impl;

import com.domain.common.Const;
import com.domain.common.Result;
import com.domain.dao.*;
import com.domain.pojo.Users;
import com.domain.pojo.UsersAddFriends;
import com.domain.service.H5Service;
import com.domain.utils.DateUtils;
import com.domain.utils.NotNullUtils;
import com.domain.utils.RelativeDateFormat;
import com.domain.utils.UUIDUtils;
import com.domain.utils.wyyx.IMSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class H5ServiceImpl implements H5Service {

    @Autowired
    private UsersHelpMapper usersHelpMapper;
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UsersAddFriendsMapper usersAddFriendsMapper;

    @Autowired
    private FollowMapper followMapper;


    /**
     * 获取帮忙详情
     *
     * @param helpUserId
     * @param publishId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Result getH5PublishDetail(String helpUserId, String publishId, String helpType) {
        Map<String, Object> p = usersHelpMapper.selectByPublishIdAndHeliUsersId(publishId, helpUserId, helpType);
        //获取帮转人信息
        Users helpUsers = usersMapper.selectByPrimaryKey(p.get("help_id").toString());
        p.put("helpUserHead", helpUsers.getHeadImage());
        p.put("helpUsersId", helpUsers.getId());
        p.put("helpUserName", helpUsers.getName());
        p.put("images", NotNullUtils.sliptList(p.get("images").toString()));
        p.put("personalLabel", NotNullUtils.sliptList(p.get("personalLabel").toString()));
        p.put("publishTime", RelativeDateFormat.format(DateUtils.strToDate(p.get("publishTime").toString(), "yyyy-MM-dd HH:mm:ss")));
        p.put("birthday", DateUtils.getAgeByBirthDay(DateUtils.strToDate(p.get("birthday").toString(), "yyyy-MM-dd")));
        p.put("commentSize", commentMapper.selectBypublishId(p.get("publishId").toString()).size());
        //关注的次数
        p.put("followSize", followMapper.selectByPublishId(publishId).size());
        //是否是素公益
        if (p.get("pstate").equals("3")) {
            p.put("pstate", true);
        } else {
            p.put("pstate", false);
        }
        return Result.success(p);
    }

    /**
     * h5注册
     *
     * @param phoneNum
     * @return
     */
    @Override
    public Result h5Regester(String phoneNum, String helpUsersId, String smsCode) {
        //判断该手机号是否注册
        Users userss = usersMapper.selectByUserPhone(phoneNum);
        if (!NotNullUtils.isNotNullObject(userss)) {
            Users users = new Users();
            String uuid = UUIDUtils.getRandomUUID();
            String uuidtoken = UUIDUtils.getRandomUUID();
            users.setId(UUIDUtils.getRandomUUID());
            users.setPhone(phoneNum);
            users.setSpareOne(uuidtoken);
            try {
                IMSet.createUser(uuid, "", "", "", "", "", uuidtoken);
            } catch (IOException e) {
                e.printStackTrace();
            }
            int result = usersMapper.insertSelective(users);
            //注册成功添加注册人好友请求信息
            if (result > 0) {
                UsersAddFriends ud = new UsersAddFriends();
                ud.setId(UUIDUtils.getRandomUUID());
                ud.setType(Const.REQUEST_FRIENDS);
                ud.setCreatTime(new Date());
                ud.setUsersId(uuid);
                ud.setFriendsId(helpUsersId);
                ud.setSpareOne("来自通讯录请求加好友");
                usersAddFriendsMapper.insertSelective(ud);
            }
        }
        return Result.success();
    }
}
